package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Psycho
 * @create 2022-04-25-18:24
 */
@RestController
@RequestMapping("/api/v1/channel")
public class WmChannelController {

    @Autowired
    private WmChannelService wmChannelService;

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @GetMapping("/channels")
    public ResponseResult getChannels(){
        return wmChannelService.getChannels();
    }

    /**
     * 查询敏感词
     * @param dto
     * @return
     */
    @PostMapping("/listChannels")
    public Page listSensitive(@RequestBody SensitiveDto dto){
        if(StringUtils.isBlank(dto.getName())){
            Page page = new Page(dto.getPage(),dto.getSize());
            return wmChannelMapper.selectPage(page, Wrappers.<WmChannel>lambdaQuery().select());
        }
        Page page = new Page(dto.getPage(),dto.getSize());
        return wmChannelMapper.selectPage(page,Wrappers.<WmChannel>lambdaQuery().select().like(WmChannel::getName,dto.getName()));
    }

    /**
     * 删除某个敏感词
     * @param id
     * @return
     */
    @GetMapping("del/{id}")
    public ResponseResult deleteOneSensitive(@PathVariable("id")Integer id){
        return ResponseResult.okResult(wmChannelMapper.delete(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId,id)));
    }
}
